﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wicresoft.SMS.Common;
using Wicresoft.SMS.Data;

namespace Wicresoft.SMS.Gateway.Host
{
    class StateCheckerTask:Task
    {
        protected IServiceConfigProvider _configProvider;
        protected IEmailDataAccess _emaildataaccess;
        protected IStateCheck _statechecker;
        protected IExchangeServerInfo _esinfo;
        private bool isonline = true;

        public StateCheckerTask(IServiceConfigProvider iscp, IEmailDataAccess email, IStateCheck check, IExchangeServerInfo esinfo)
        {
            //DBLog.Info("StateCheckerTask StateCheckerTask");
            this._configProvider = iscp;
            this._emaildataaccess = email;
            this._statechecker = check;
            this._esinfo = esinfo;
            MailOperation.InstanceSmtpClient(_esinfo.ESUsername, _esinfo.ESPassword, _esinfo.ESHost, _esinfo.ESPort, _esinfo.ESEmail);
        }

        protected override void ExecuteCore()
        {
           // DBLog.Info("StateCheckerTask ExecuteCore");
            DBLog.Info("_scanAble: " + _scanAble.ToString());
            DBLog.Info("_configProvider.StateCheckerTaskEnable: " + _configProvider.StateCheckerTaskEnable.ToString());

            while (_scanAble && _configProvider.StateCheckerTaskEnable == 1)
            {
                Check();
                System.Threading.Thread.Sleep(
                    _configProvider.StateCheckerTaskSpan < GlobalFactory.TaskSpanBorder ? GlobalFactory.TaskSpanBorder : _configProvider.StateCheckerTaskSpan);
            }
        }

        private void Check()
        {
            try
            {
                DBLog.Debug("Entrying StateCheckerTask");
               // DBLog.Debug("StateChecker StateCheckerTimeSpan " + _configProvider.StateCheckerTimeSpan);
               // DBLog.Debug("StateChecker StateCheckerPendingLimit " + _configProvider.StateCheckerPendingLimit);
               // DBLog.Debug("StateChecker StateCheckerAdminsLow " + _configProvider.StateCheckerAdminsLow);
              //  DBLog.Debug("StateChecker WebUrl " + _configProvider.WebUrl);
               // DBLog.Debug("StateChecker StateCheckerAdminsHigh " + _configProvider.StateCheckerAdminsHigh);
                List<Email> emails = _statechecker.CheckStates(_configProvider.StateCheckerTimeSpan, _configProvider.StateCheckerPendingLimit, _configProvider.StateCheckerAdminsLow, _configProvider.WebUrl);
                if (emails != null)
                {
                    DBLog.Debug("StateChecker Email count " + emails.Count.ToString());
                    if (isonline == false)
                    {
                        isonline = true;
                        Common.EmailInfo ei = new Common.EmailInfo();
                        ei.To = _configProvider.StateCheckerAdminsHigh;
                        ei.Subject = "提醒：系统数据库访问恢复正常 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        ei.Body = "系统数据库访问恢复正常! ";
                        MailOperation.SendEmail(ei);
                        DBLog.Info("系统数据库访问恢复正常!");
                    }
                    _emaildataaccess.SendEmail(emails);
                }
                else
                {
                    if (isonline == true)
                    {
                        Common.EmailInfo ei = new Common.EmailInfo();
                        ei.To = _configProvider.StateCheckerAdminsHigh;
                        ei.Subject = "警报! 系统无法访问数据库! " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        ei.Body = "系统无法访问数据库, 请检查! ";

                        MailOperation.SendEmail(ei);
                        isonline = false;
                    }

                }
                DBLog.Debug("Leaving StateCheckerTask");
            }
            catch (Exception ex)
            {
                //Wicresoft.SMS.Common.Logger.Error("StateCheckerTask.Check " + ex.ToString());
                DBLog.Exception("StateCheckerTask.Check ", ex);

            }           

        }
    }
}
